package com.qeeyou.apps.android.craftsman.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;

/**
 * 自定义TabbarButton
 * Created by xuchengxiong on 2017/12/16.
 */

public class PublishActionButton extends LinearLayout {

    private LinearLayout view;
    private ImageView imageView;
    private TextView titleView;
    private TextView textView;

    private int image;
    private String text;
    private String title;
    private int imageWidth;
    private int imageHeight;
    private int background;

    public PublishActionButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.publishActionButton);
        image = typedArray.getResourceId(R.styleable.publishActionButton_image, R.drawable.ic_publish_gongzhuang);
        text = typedArray.getString(R.styleable.publishActionButton_text);
        title = typedArray.getString(R.styleable.publishActionButton_title);
        imageWidth = (int)typedArray.getDimension(R.styleable.publishActionButton_imageWidth, 60);
        imageHeight = (int)typedArray.getDimension(R.styleable.publishActionButton_imageHeight, 60);
        background = typedArray.getResourceId(R.styleable.publishActionButton_background, R.color.colorWhite);
        //TODO -- 初始化
        initPublishButton(context);
        typedArray.recycle();

        this.setFocusable(true);
        this.setClickable(true);
    }

    private void initPublishButton(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = (LinearLayout) inflater.inflate(R.layout.view_publish_button, this, true);
        this.setBackgroundResource(background);
        imageView = view.findViewById(R.id.image4PublishButton);
        LayoutParams layoutParams = new LayoutParams(imageWidth, imageHeight);
        int leftMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,context.getResources().getDisplayMetrics());
        int rightMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,context.getResources().getDisplayMetrics());
        int topMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,context.getResources().getDisplayMetrics());
        int bottomMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6,context.getResources().getDisplayMetrics());
        layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        imageView.setLayoutParams(layoutParams);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        titleView = view.findViewById(R.id.title4PublishButton);
        textView = view.findViewById(R.id.text4PublishButton);
        //将自定义的属性值传递给view
        imageView.setImageResource(image);
        titleView.setText(title);
        textView.setText(text);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
